package com.mycompany.myapp.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.mycompany.myapp.domain.ElecBicycleUser;
import com.mycompany.myapp.repository.ElecBicycleUserRepository;
import com.mycompany.myapp.web.rest.errors.BadRequestAlertException;
import com.mycompany.myapp.web.rest.util.HeaderUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing ElecBicycleUser.
 */
@RestController
@RequestMapping("/api")
public class ElecBicycleUserResource {

    private final Logger log = LoggerFactory.getLogger(ElecBicycleUserResource.class);

    private static final String ENTITY_NAME = "elecBicycleUser";

    private final ElecBicycleUserRepository elecBicycleUserRepository;

    public ElecBicycleUserResource(ElecBicycleUserRepository elecBicycleUserRepository) {
        this.elecBicycleUserRepository = elecBicycleUserRepository;
    }

    /**
     * POST  /elec-bicycle-users : Create a new elecBicycleUser.
     *
     * @param elecBicycleUser the elecBicycleUser to create
     * @return the ResponseEntity with status 201 (Created) and with body the new elecBicycleUser, or with status 400 (Bad Request) if the elecBicycleUser has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/elec-bicycle-users")
    @Timed
    public ResponseEntity<ElecBicycleUser> createElecBicycleUser(@RequestBody ElecBicycleUser elecBicycleUser) throws URISyntaxException {
        log.debug("REST request to save ElecBicycleUser : {}", elecBicycleUser);
        if (elecBicycleUser.getId() != null) {
            throw new BadRequestAlertException("A new elecBicycleUser cannot already have an ID", ENTITY_NAME, "idexists");
        }
        ElecBicycleUser result = elecBicycleUserRepository.save(elecBicycleUser);
        return ResponseEntity.created(new URI("/api/elec-bicycle-users/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /elec-bicycle-users : Updates an existing elecBicycleUser.
     *
     * @param elecBicycleUser the elecBicycleUser to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated elecBicycleUser,
     * or with status 400 (Bad Request) if the elecBicycleUser is not valid,
     * or with status 500 (Internal Server Error) if the elecBicycleUser couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/elec-bicycle-users")
    @Timed
    public ResponseEntity<ElecBicycleUser> updateElecBicycleUser(@RequestBody ElecBicycleUser elecBicycleUser) throws URISyntaxException {
        log.debug("REST request to update ElecBicycleUser : {}", elecBicycleUser);
        if (elecBicycleUser.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        ElecBicycleUser result = elecBicycleUserRepository.save(elecBicycleUser);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, elecBicycleUser.getId().toString()))
            .body(result);
    }

    /**
     * GET  /elec-bicycle-users : get all the elecBicycleUsers.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of elecBicycleUsers in body
     */
    @GetMapping("/elec-bicycle-users")
    @Timed
    public List<ElecBicycleUser> getAllElecBicycleUsers() {
        log.debug("REST request to get all ElecBicycleUsers");
        return elecBicycleUserRepository.findAll();
    }

    /**
     * GET  /elec-bicycle-users/:id : get the "id" elecBicycleUser.
     *
     * @param id the id of the elecBicycleUser to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the elecBicycleUser, or with status 404 (Not Found)
     */
    @GetMapping("/elec-bicycle-users/{id}")
    @Timed
    public ResponseEntity<ElecBicycleUser> getElecBicycleUser(@PathVariable Long id) {
        log.debug("REST request to get ElecBicycleUser : {}", id);
        Optional<ElecBicycleUser> elecBicycleUser = elecBicycleUserRepository.findById(id);
        return ResponseUtil.wrapOrNotFound(elecBicycleUser);
    }

    /**
     * DELETE  /elec-bicycle-users/:id : delete the "id" elecBicycleUser.
     *
     * @param id the id of the elecBicycleUser to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/elec-bicycle-users/{id}")
    @Timed
    public ResponseEntity<Void> deleteElecBicycleUser(@PathVariable Long id) {
        log.debug("REST request to delete ElecBicycleUser : {}", id);

        elecBicycleUserRepository.deleteById(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
